<?php 
if(isset($_REQUEST['fuseaction_back'])) {
	$fuseaction_back=$_REQUEST['fuseaction_back'];
}else{
	$fuseaction_back = "";
}

$v_recordtype_filter = $_SESSION['onegate_current_recordtype'];
$v_staff_id = $_SESSION['staff_id'];
$v_current_status = ($arr_single_record['C_CURRENT_STATUS']);
$v_detail_status = intval($arr_single_record['C_DETAIL_STATUS']);
$v_recordtype_name = Get_text_from_array($_SESSION['recordtype'],'PK_RECORDTYPE','PK_RECORDTYPE','C_NAME',$v_recordtype_filter);
$v_record_code_list='';
for($row_index = 0;$row_index< $v_count;$row_index++){
	if ($row_index<$v_count-1)
		$v_record_code_list = $v_record_code_list.$arr_all_record[$row_index][0].", ";
	else
		$v_record_code_list = $v_record_code_list.$arr_all_record[$row_index][0];
}

$arr_record_id_list = split(',',$v_record_id_list);

/*
echo '$v_recordtype_filter ='.$v_recordtype_filter.'<br>';
echo '$v_staff_id ='.$v_staff_id.'<br>';
echo '$v_current_status ='.$v_current_status.'<br>';
echo '$v_detail_status ='.$v_detail_status.'<br>';
echo '$v_recordtype_name ='.$v_recordtype_name.'<br>';
echo '$v_record_code_list ='.$v_record_code_list.'<br>';
*/


	//cac file chua cau truc cua bang
	$v_table_struct_xml_file = "../../xml/".$v_recordtype_filter."/cau_truc_bang_noi_dung_giay_phep_nhieu_ho_so.xml";
	$v_formfield_xml_file = "../../xml/".$v_recordtype_filter."/cac_dong_cua_cap_nhat_noi_dung_giay_phep.xml";
	//echo $v_formfield_xml_file; exit;
	if (file_exists($v_table_struct_xml_file) && file_exists($v_formfield_xml_file)){
		$v_table_struct_xml_string = _read_file($v_table_struct_xml_file);
		$v_formfield_xml_string = _read_file($v_formfield_xml_file);
	}else{
		$v_table_struct_xml_file = "../../xml/other/cau_truc_bang_noi_dung_giay_phep_nhieu_ho_so.xml";
		$v_formfield_xml_file = "../../xml/other/cac_dong_cua_noi_dung_giay_phep.xml";
		$v_table_struct_xml_string = _read_file($v_table_struct_xml_file);
		$v_formfield_xml_string = _read_file($v_formfield_xml_file);
	}
	$v_table_struct_xml_string = _read_file($v_table_struct_xml_file);
	$v_formfield_xml_string = _read_file($v_formfield_xml_file);
	//echo htmlspecialchars($v_formfield_xml_string);
	//Doc va ghi ra tieu de cua form
	$table_struct_rax = new RAX(); 
	$table_struct_rec = new RAX(); 
	$table_struct_rax->open($v_table_struct_xml_string);
	$table_struct_rax->record_delim = 'common_para';
	$table_struct_rax->parse();
	$table_struct_rec = $table_struct_rax->readRecord(); 
	$table_struct_row = $table_struct_rec->getRow();
	$v_form_title = $table_struct_row["form_title"];
	$v_first_col_width = $table_struct_row["first_col_width"];
	$v_second_col_width = (100-$v_first_col_width)."%";
	$v_js_file_name = $table_struct_row["js_file_name"];
	$v_js_function  = $table_struct_row["js_function"];
	///Begin//Form
	$v_html_string = '<script src="../js-record/'.$v_js_file_name.'"></script>';
	//Begin//Bang chua cac thanh phan cua form
	$v_html_string = $v_html_string . "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr>";
	$v_html_string = $v_html_string . "<td class='large_title'>".CONST_UPDATE_MULTIPLE_RECORD_TITLE."</td>";
	$v_html_string = $v_html_string . "</tr></table>";
	
	$v_html_string = $v_html_string ."<table width='100%' class='form_table1'  border='0' cellspacing='0' cellpadding='0'><tr><td>";
	//Begin//Bang chua cac dong co dinh cua ho so
	$v_html_string = $v_html_string . "<table width='100%'  border='0' cellspacing='0' cellpadding='0'>";
	$v_html_string = $v_html_string . "<col width='18%'><col width='32%'><col width='31%'><col width='19%'>";
	$v_html_string = $v_html_string . "<tr><td class='normal_label'>".CONST_RECORD_TYPE_LABEL."</td>";
	$v_html_string = $v_html_string . "<td class='normal_label'>".$v_recordtype_name."</td></tr>";
	$v_html_string = $v_html_string . "<tr><td class='normal_label'>".CONST_LIST_RECORD_LABEL."</td>";
	$v_html_string = $v_html_string . "<td class='normal_label'>".$v_record_code_list."</td></tr>";
	$v_html_string = $v_html_string . "</table>";
	$v_html_string = $v_html_string . "</td></tr></table>";
	//echo htmlspecialchars($v_html_string);exit;
	//End//Bang chua cac dong co dinh cua ho so
	echo $v_html_string.'<br>';

	//========================================================
	//Lay du lieu noi dung giay phep tu database cua tung ho so
	for($row_index = 0;$row_index< $v_count;$row_index++){
		$v_record_id = '';
		$v_record_id = $arr_all_record[$row_index][2];
		
		//Lay noi dung giay phep cua ho so, neu ho so chua co noi dung giay phep thi lay chuoi xml ben duoi
		if ($v_record_id>0){
			$v_code_record = $arr_all_record[$row_index][0];
		
			if($arr_all_record[$row_index][15]!=''){
				$v_license_xml_data = $arr_all_record[$row_index][15];
			}else {
				$v_license_xml_data = '<?xml version="1.0" encoding="UTF-8"?><root><data_list></data_list></root>';
			}
		}
		if ($v_record_id_get == $arr_all_record[$row_index][2]) { 
			$v_xml_str = $arr_all_record[$row_index][14];
		} else {
			$v_xml_str = $v_license_xml_data;
		}
		//echo htmlspecialchars($arr_all_record[$row_index][2]);
	
		$v_current_status = ($arr_all_record[$row_index][17]);
		$v_detail_status = intval($arr_all_record[$row_index][16]);
		$v_staff_id = _get_item_attr_by_id ($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'id');
		
		//Kiem tra NSD hien thoi dong vai tro la thu ly chinh doi voi ho so hien thoi va trang thai cua ho so la "CAP_PHEP" va C_DETAIL_STATUS <>20
		if ( (onegate_is_main_handler_for_record($v_staff_id,$v_record_id)) && $v_detail_status <> 20){
			$v_is_main_handler_for_record_and_status = 1;
		}else{
			$v_is_main_handler_for_record_and_status = 0;
		}
	
		//Kiem tra NSD hien thoi dong vai tro la thu ly chinh doi voi ho so hien thoi va trang thai cua ho so la "DANG_THU_LY" va 	C_DETAIL_STATUS <>20
		if ( (onegate_is_main_handler_for_record($v_staff_id,$v_record_id))&& $v_current_status == "DANG_THU_LY" && $v_detail_status <> 20){
			$v_is_main_handler_for_record = 1;
		}else{
			$v_is_main_handler_for_record = 0;
		}	
		
		
		$v_process_date = 0;
		if ($arr_all_recordtype[0][0]==$v_recordtype_filter){
			$v_process_date = $arr_all_recordtype[0][4];
		}
		
		if (($v_recordtype_filter == 'other') || ($v_is_main_handler_for_record_and_status ==0)) {
			$v_display = "none";
		}else{
			$v_display = "block";
		}
		
		/*	
		echo '----------------------------'	.'<br>';
		//var_dump($arr_all_record[$row_index]).'<br>';
		//var_dump($arr_all_recordtype).'<br>';
		//echo '$v_license_xml_data ='.htmlspecialchars($v_license_xml_data).'<br>';
		echo '$v_recordtype_filter ='.$v_recordtype_filter.'<br>';
		echo '$v_record_id ='.$v_record_id.'<br>';
		echo '$v_code_record ='.$v_code_record.'<br>';
		echo '$v_current_status ='.$v_current_status.'<br>';
		echo '$v_detail_status ='.$v_detail_status.'<br>';
		echo '$v_staff_id ='.$v_staff_id.'<br>';
		echo '$v_is_main_handler_for_record_and_status ='.$v_is_main_handler_for_record_and_status.'<br>';
		echo '$v_is_main_handler_for_record ='.$v_is_main_handler_for_record.'<br>';
		echo '$v_process_date ='.$v_process_date.'<br>';
		echo '$v_display ='.$v_display.'<br>';
		echo '----------------------------'	.'<br>';
		*/
		
		//Bang chua cac bien hidden
		$v_html_string = "<table width='100%' cellpadding='0' cellspacing='0'><tr><td>";
		$v_html_string = $v_html_string . "<form action='index.php' method='post' name='f_dsp_single_license'>";
		$v_html_string = $v_html_string . "<input type='hidden' name='hdn_list_xml_tag_".$row_index."' value=''>";
		$v_html_string = $v_html_string . "<input type='hidden' name='hdn_list_xml_value_".$row_index."' value=''>";
		$v_html_string = $v_html_string . "<input type='hidden' name='fuseaction' value='DISPLAY_MULTIPLE_LICENSE'>";
		$v_html_string = $v_html_string . "<input type='hidden' name='hdn_record_id_".$row_index."' value='".$v_record_id ."'>";
		$v_html_string = $v_html_string . "<input type='hidden' name='hdn_record_id_list' value='".$v_record_id_list ."'>";
		$v_html_string = $v_html_string . "<input type='hidden' name='hdn_recordtype_filter' value='".$v_recordtype_filter ."'>";
		$v_html_string = $v_html_string . "<input type='hidden' name='hdn_isa_calendar_path' value='".$_ISA_LIB_URL_PATH."isa-calendar/'>";
		$v_html_string = $v_html_string . "<input type='hidden' name='fuseaction_back' value='".$fuseaction_back."'>";
		$v_html_string = $v_html_string . "</td></tr></table>";
		
		//Doc cac dong cua form	
		$table_struct_rax = new RAX(); 
		$table_struct_rec = new RAX(); 
		$table_struct_rax->open($v_table_struct_xml_string);
		$table_struct_rax->record_delim = 'row';
		$table_struct_rax->parse();
		$table_struct_rec = $table_struct_rax->readRecord(); 
			
		//Bang chua cac dong cua form
		$v_html_string = $v_html_string . "<table width='100%' class='form_table1'  border='0' cellspacing='0' cellpadding='0'><tr><td>";
		$v_html_string = $v_html_string . "<table width='100%'  border='0' cellspacing='0' cellpadding='0'>";
		$v_html_string = $v_html_string . "<col width='20%'><col width='80%'>";
		$v_html_string = $v_html_string . "<tr><td class='normal_label'>".CONST_RECORD_LABEL_2."</td>";
		$v_html_string = $v_html_string . "<td class='normal_label'>".$v_code_record."</td></tr>";
		$v_html_string = $v_html_string . "</table>";
		
		while ($table_struct_rec) { 
			$table_struct_row = $table_struct_rec->getRow();
			$v_have_line_before = $table_struct_row["have_line_before"];
			$v_col_number = $table_struct_row["col_number"];
			$v_tag_list = $table_struct_row["tag_list"];
			$v_row_id = $table_struct_row["row_id"];
			$arr_tag = explode(",", $v_tag_list); //Mang tung dong hien thi
			if ($v_have_line_before=="true"){
				$v_html_string = $v_html_string . "<table width='100%'  border='0' cellspacing='0' cellpadding='0'><tr>";
				$v_html_string = $v_html_string . "<td><hr width='100%' color='#66CCFF' size='1'></td>";
				$v_html_string = $v_html_string . "</tr></table>";
			}
			//Bang chua mot dong cua form
			$v_html_string = $v_html_string . "<table width='100%'  border='0' cellspacing='0' cellpadding='0'>";
			$v_html_table = "";
			$v_html_tag = "";									
			for($i=0;$i < sizeof($arr_tag);$i++){
				$formfield_rax = new RAX(); 
				$formfield_rec = new RAX(); 
				$formfield_rax->open($v_formfield_xml_string);
				$formfield_rax->record_delim = $arr_tag[$i];
				$formfield_rax->parse();
				$formfield_rec = $formfield_rax->readRecord(); 
				$formfield_row = $formfield_rec->getRow(); 
				$v_label = $formfield_row["label"];
				$v_type = $formfield_row["type"];
				$v_dataformat = $formfield_row["data_format"];
				$v_width = $formfield_row["width"];
				$v_row = $formfield_row["row"];
				$v_max = $formfield_row["max"];
				$v_min = $formfield_row["min"];
				$v_maxlength = $formfield_row["maxlength"];
				$v_note = $formfield_row["note"];
				$v_message = $formfield_row["message"];
				$v_optional = $formfield_row["optional"];
				$v_xml_tag_in_db = $formfield_row["xml_tag_in_db"];
				$v_js_function_list = $formfield_row["js_function_list"];
				$v_js_action_list = $formfield_row["js_action_list"];
				$v_readonly_in_edit_mode = $formfield_row["readonly_in_edit_mode"];
				$v_disabled_in_edit_mode = $formfield_row["disabled_in_edit_mode"];
				$v_hide = $formfield_row["hide"];
				
				if($v_hide=="true"){
					$v_style = "none";
				}else{
					$v_style = "block";
				}
							
				if ($v_xml_str != ""){
					$column_rax = new RAX(); 
					$column_rec = new RAX();
					$column_rax->open($v_xml_str);
					$column_rax->record_delim = 'data_list';
					$column_rax->parse();
					$column_rec = $column_rax->readRecord(); 
					$column_row = $column_rec->getRow();
					$v_value = _restore_XML_bad_char($column_row[$v_xml_tag_in_db]); 
				}
				if ($v_type=="selectbox"){
					$v_selectbox_option_sql = $formfield_row["selectbox_option_sql"];
					$v_selectbox_id_column = $formfield_row["selectbox_option_id_column"];
					$v_selectbox_name_column = $formfield_row["selectbox_option_name_column"];
				}
				if ($v_type=="checkboxmultiple"){
					$v_checkbox_multiple_sql = $formfield_row["checkbox_multiple_sql"];
					$v_checkbox_multiple_id_column = $formfield_row["checkbox_multiple_id_column"];
					$v_checkbox_multiple_name_column = $formfield_row["checkbox_multiple_name_column"];
				}
				$v_html_table = $v_html_table . "<col width='$v_first_col_width'>" . "<col width='$v_second_col_width'>";
				$v_xml_tag_in_db = $v_xml_tag_in_db.'_'.$row_index;		
				$v_html_tag = $v_html_tag . Generate_HTML_Input();		
			}
			$v_html_string = $v_html_string .  $v_html_table . "<tr id = '$v_row_id' style='display:".$v_style."'>" . "<td class='normal_label'>" . $v_html_tag."</td>";
			$v_html_string = $v_html_string . "</tr></table>";
			$table_struct_rec = $table_struct_rax->readRecord();
		}
		$v_html_string = $v_html_string . "</td></tr>";
		$v_html_string = $v_html_string . '<script language="JavaScript">'.$v_js_function.'</script>';
			
		//End//Bang chua cac thanh phan cua form
		//print form
		
		echo $v_html_string;
	?>
		<tr><td>&nbsp;</td></tr>
		<tr align="center">
			<td><?php
				if((($v_is_granted_update) || ($v_is_main_handler_for_record_and_status)|| ($v_is_main_handler_for_record) || onegate_is_handler_leader_for_recordtype($v_staff_id,$v_recordtype_filter,$_SESSION["recordtype"]))){
				$btn_update = 'btn_update_'.$row_index;
				?>
					<div id="load_<?=$v_record_id?>" style="display:none"><img src="../../images/load.gif" align="middle"><p class="owner_line1">&#272;ang s&#7855;p x&#7871;p</p></img></div>
					<input type='button' class='normal_button' name='<?php echo $btn_update;?>' value='<?php echo _CONST_SAVE_BUTTON;?>' onClick="check_value_in_form();save_list_onclick(document.forms(<?php echo $row_index;?>),document.forms(<?php echo $row_index;?>).hdn_list_xml_tag_<?php echo $row_index;?>,document.forms(<?php echo $row_index;?>).hdn_list_xml_value_<?php echo $row_index;?>); update_license_record(document.forms(<?php echo $row_index;?>).hdn_record_id_<?php echo $row_index;?>,document.forms(<?php echo $row_index;?>).hdn_list_xml_tag_<?php echo $row_index;?>,document.forms(<?php echo $row_index;?>).hdn_list_xml_value_<?php echo $row_index;?>);" onKeyDown="change_focus(document.forms(<?php echo $row_index;?>),this)">&nbsp;
					<input type = 'button' class='normal_button' style='width:20%; display: $v_display' name='btn_query_data' value='<?=CONST_QUERY_BUTTON?>' onClick="btn_query_onclick_multilicense('<?=$v_record_id?>')"><?php
				}?>
				<input type="button" value="<?php echo _CONST_BACK_BUTTON; ?>" name="btn_add" class="normal_button" onClick="btn_back_onclick('<?php echo $fuseaction_back?>');">
			</td>	
		</tr>
	</table>
	<br>
	</form>
	<?php } ?>
<!--//End//Form-->

<script language="JavaScript">
	set_focus(document.forms(0));
</script>